package com.xhl.noval.api.core.entity;

/**
 * @author xionghuilong
 * @description: 返参
 * @date 2021/12/3 14:14
 */

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.xhl.noval.api.core.exception.BaseExceptionCode;
import com.xhl.noval.api.core.exception.ExceptionCode;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

@JsonIgnoreProperties(
        ignoreUnknown = true
)
public class ApiResult<T> implements Serializable {
    public static final String PROPERTY_CODE = "code";
    public static final String PROPERTY_MSG = "msg";
    private static final long serialVersionUID = 2169343367871161215L;
    private String code;
    private String msg;
    private T data;
    @ApiModelProperty("请求路径")
    private String path;

    public ApiResult() {
        this.code = "1";
        this.msg = "success";
    }

    public ApiResult(T data) {
        this.code = "1";
        this.msg = "success";
        this.data = data;
    }

    public ApiResult(String code, String message) {
        this.code = "1";
        this.msg = "success";
        this.code = code;
        this.msg = message;
    }

    public ApiResult(Integer code, String message) {
        this(String.valueOf(code), message);
    }

    public ApiResult(String code, String message, T data) {
        this.code = "1";
        this.msg = "success";
        this.code = code;
        this.msg = message;
        this.data = data;
    }

    public boolean isSuccess() {
        return "1".equals(this.code);
    }

    public String toString() {
        return JSONObject.toJSONString(this);
    }

    public static <E> ApiResult<E> success(E data, String msg) {
        return new ApiResult("1", msg, data);
    }

    public static <E> ApiResult<E> success(E data) {
        return new ApiResult("1", (String) null, data);
    }

    public static <E> ApiResult<E> fail(Throwable throwable) {
        return fail("-1", throwable != null ? throwable.getMessage() : "系统繁忙，请稍候再试");
    }

    public static <E> ApiResult<E> fail(String code, String msg) {
        return new ApiResult(code, msg != null && !msg.isEmpty() ? msg : "系统繁忙，请稍候再试");
    }

    public static <E> ApiResult<E> fail(String msg) {
        return fail("-1", msg);
    }

    public static <E> ApiResult<E> fail(BaseExceptionCode exceptionCode) {
        return validFail(exceptionCode);
    }

    public static <E> ApiResult<E> fail(String msg, Object... args) {
        String message = msg != null && !msg.isEmpty() ? msg : "系统繁忙，请稍候再试";
        return new ApiResult("-1", String.format(message, args));
    }

    public static <E> ApiResult<E> result(String code, E data, String msg) {
        return new ApiResult(code, msg, data);
    }

    public static <E> ApiResult<E> validFail(BaseExceptionCode exceptionCode) {
        return new ApiResult(exceptionCode.getCode(), exceptionCode.getMsg() != null && !exceptionCode.getMsg().isEmpty() ? exceptionCode.getMsg() : "系统繁忙，请稍候再试");
    }

    public static <E> ApiResult<E> validFail(String msg) {
        return new ApiResult(ExceptionCode.BASE_VALID_PARAM.getCode(), msg != null && !msg.isEmpty() ? msg : "系统繁忙，请稍候再试");
    }

    public static <E> ApiResult<E> validFail(String msg, Object... args) {
        String message = msg != null && !msg.isEmpty() ? msg : "系统繁忙，请稍候再试";
        return new ApiResult(ExceptionCode.BASE_VALID_PARAM.getCode(), String.format(message, args));
    }

    public static <E> ApiResult<E> successDef(E data) {
        return new ApiResult("1", (String) null, data);
    }

    public static <E> ApiResult<E> successDef() {
        return new ApiResult("1", (String) null, (Object) null);
    }

    public static <E> ApiResult<E> timeout() {
        return fail((BaseExceptionCode) ExceptionCode.TIMEOUT);
    }

    public String getCode() {
        return this.code;
    }

    public String getMsg() {
        return this.msg;
    }

    public T getData() {
        return this.data;
    }

    public String getPath() {
        return this.path;
    }

    public ApiResult<T> setCode(final String code) {
        this.code = code;
        return this;
    }

    public ApiResult<T> setMsg(final String msg) {
        this.msg = msg;
        return this;
    }

    public ApiResult<T> setData(final T data) {
        this.data = data;
        return this;
    }

    public ApiResult<T> setPath(final String path) {
        this.path = path;
        return this;
    }
}
